import { getOrderDetail, addOrder, payOrder, editOrder } from "../api/index";
import { store } from "../../store/index";
import { pay } from "../utils/commom";
import { gameTitle } from '../utils/enumType';

Page({
  data: {
    id: "",
    pageData: {},
    formData: {
      remark: "",
      fee: "",
      timeDemand: "",
    },
    calculatedTotal: 0,
    selectCoupon: null,
    gameTitle: gameTitle

  },

  onLoad(options) {
    if (options.id) {
      this.setData({
        id: options.id,
      });
      this.getOrderDetail();
    } else {
      this.getCalculatedTotal({
        fee: store.orderData.totalPrice,
        discount: store.selectCoupon?.discount || 0,
      });
      console.log(store.orderData, 'store.orderData');
      this.setData({
        pageData: {
          ...store.orderData,
          titleType: store.orderData.type.split("")[0],
        },
      });
    }
  },
  onShow() {
    if (store.selectCoupon) {
      console.log(store.selectCoupon, "store.selectCoupon");
      this.setData({
        selectCoupon: store.selectCoupon,
      });
      this.getCalculatedTotal({
        discount: store.selectCoupon.discount,
        fee: this.data.formData.fee || 0,
      });
    }
  },
  onUnload() {
    store.updateSelectCoupon(null);
  },
  getOrderDetail() {
    getOrderDetail({ id: this.data.id }).then((res) => {
      this.setData({
        pageData: {
          ...res.result,
          titleType: res.result.type.split("")[0],
        },
        formData: {
          ...res.result,
        }
      });
    });
  },
  getCalculatedTotal(data) {
    console.log(data, "data");
    this.setData({
      calculatedTotal:
        (Number(this.data.pageData.totalPrice) || 0) +
        (Number(data.fee) || 0) -
        (Number(data.discount) || 0),
    });
  },
  handleInput(e) {
    console.log(e, "e");
    const key = e.currentTarget.dataset.key;
    const value = e.detail.value;
    if (key === "fee") {
      this.getCalculatedTotal({
        fee: value,
        discount: this.data?.selectCoupon?.discount || 0,
      });
    }
    this.setData({
      [`formData.${key}`]: value,
    });
  },
  handleCoupon() {
    if (this.data.id) {
      return
    }
    wx.navigateTo({
      url: "/pages/me_content/discount?type=select",
    });
  },
  handleCancel() {
    wx.showModal({
      title: '确定取消订单吗？',
      content: '取消订单后，订单将不再显示',
      success: (res) => {
        if (res.confirm) {
          editOrder({
            id: this.data.id,
            status: '取消'
          }).then(() => {
            wx.showToast({
              title: '取消订单成功',
              icon: 'none',
            });
            wx.navigateBack({
              delta: 1
            })
          })
        }
      }
    })
    // wx.navigateBack({
    //   delta: 1
    // })
  },
  handlePay() {
    if (this.data.id) {
      payOrder({
        ...this.data.formData,
      }).then(() => {
        wx.showToast({
          title: "支付成功",
          icon: "success",
        });
        wx.navigateBack({
          delta: 1,
        });
      });
    } else {
      // if (!this.data.formData.timeDemand) {
      //   wx.showToast({
      //     title: "请输入时间要求",
      //     icon: "none",
      //   });
      //   return;
      // }

      const data = {
        ...this.data.formData,
        ...this.data.pageData,
        receiverMark: this.data.formData.remark || '',
        totalPrice: this.data.calculatedTotal,
        discountId: this.data.selectCoupon?.id
      }

      switch(this.data.pageData.type) {
        case '家电服务':
          data.cleanImg = this.data.pageData.imgList
          break
        case '保洁服务':
          data.imgList = this.data.pageData.imgList
          break
        case '代扔垃圾':
          data.imgList = this.data.pageData.imgList
          break
      }



      // return
      addOrder(data).then((res) => {
        wx.showToast({
          title: "创建订单成功",
          icon: "none",
        });
    
        payOrder({
          id: res.result
        }).then(() => {

          wx.showToast({
            title: "支付成功",
            icon: "success",
          });
          wx.navigateBack()
        }).catch(() => {
          wx.showToast({
            title: "支付失败",
            icon: "none",
          });
        })
        



        // wx.navigateBack({
        //   delta: 1
        // })
      });
    }
  },
});
